package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckSetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = CheckSetmealSerivce.class)
@Transactional
public class CheckSetmealSerivceImpl implements CheckSetmealSerivce {

    @Autowired
    CheckSetmealDao  checkSetmealDao;
    //新增套餐
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        checkSetmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
//绑定套餐和检查组的多对多关系
       this.setSetmealAndCheckGroup(setmealId,checkgroupIds);
    }
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage , pageSize);
       Page<Setmeal> page =checkSetmealDao.findquery(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }


    //设置套餐和检查组多对多关系
    private void setSetmealAndCheckGroup(Integer setmealId,Integer[] checkgroupIds) {
        if (checkgroupIds != null && checkgroupIds.length > 0) {
              Map<String,Integer> map = new HashMap<>();
              map.put("setmealid",setmealId);
            for (Integer checkgroupId : checkgroupIds) {
                map.put("checkgroupid",checkgroupId);
                System.out.println(checkgroupId);
                checkSetmealDao.setSetmealAndCheckGroup(map);

            }

        }
   /* public  void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds){

        if(checkgroupIds != null && checkgroupIds.length > 0){
            for (Integer checkGroupId : checkgroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setmeal_id", setmealId);
                map.put("checkgroup_id",checkGroupId);
                checkSetmealDao.setSetmealAndCheckGroup(map);
            }

        }
    }*/
    }
}
